package com.lz.hotel_java.controller;

import com.lz.hotel_java.entity.Room;
import com.lz.hotel_java.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/rooms")
@CrossOrigin("*")
public class RoomController {
    @Autowired
    private RoomService roomService;

    @GetMapping("/all")
    public List<Room> getAllRooms() {
        return roomService.getAllRooms();
    }

    @PostMapping("/add")
    public String addRoom(@RequestBody Room room) {
        room.setStatus("空闲");
        boolean result = roomService.addRoom(room);
        return result ? "添加成功" : "添加失败";
    }

    @PostMapping("/update")
    public String updateRoom(@RequestBody Room room) {
        boolean result = roomService.updateRoom(room);
        return result ? "修改成功" : "修改失败";
    }

    @PostMapping("/delete")
    public String deleteRoom(@RequestBody Integer roomId) {
        boolean result = roomService.deleteRoom(roomId);
        return result ? "删除成功" : "删除失败";
    }
}
